<template>
	<view class="container">
		<view class="content">
			<view class="top_info">
				<view class="top_info_title">任务列表</view>
				<view class="top_info_des">每日一次机会,0点重置</view>
			</view>
			<view class="list_info">
				<view class="list_info_box" v-for="(item, index) in dataList" :key="index">
					<view class="list_info_box_icon">
						<image class="image" src="/static/images/bi_47.png" mode=""></image>
					</view>
					<view class="list_info_box_details">
						<view class="list_info_box_details_title">{{ item.name}}</view>
						<view class="list_info_box_details_des">{{ item.intro}}</view>
						<view class="list_info_box_details_progress">
							<view class="list_info_box_details_progress_default">
								<view class="list_info_box_details_progress_progress" 
								:style="{width: (item.progress * 200) + 'rpx'}"></view>
							</view>		
							<view class="list_info_box_details_progress_text" v-if="item.progress < 1">
								{{item.user_number}}/{{item.number}}
							</view>
							<view class="list_info_box_details_progress_text1 list_info_box_details_progress_text" v-else>
								{{item.user_number}}/{{item.number}}
							</view>
						</view>
					</view>
					<view class="list_info_box_button_quwancheng"
					v-if="item.task_status === 0 && item.user_number === 0"
					@click="clickToButton(item, index)">去完成</view>
					<view class="list_info_box_button_daiwancheng list_info_box_button_quwancheng"
					v-else-if="item.task_status === 0 && item.user_number > 0 && item.user_number < item.number"
					@click="clickToButton(item, index)">待完成</view>
					<view class="list_info_box_button_lingkandian list_info_box_button_quwancheng"
					v-else-if="item.task_status === 1 && item.user_number === item.number"
					@click="clickToButton(item, index)">领看币</view>
					<view class="list_info_box_button_yilingqu list_info_box_button_quwancheng"
					v-else-if="item.task_status === 2"
					@click="clickToButton(item, index)">已领取</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import interfaces from '@/utils/interfaces.js'
	import config from '@/utils/config.js'
	import tools from '@/utils/tools.js'
	export default {
		data() {
			return {
				dataList: []
			}
		},
		methods: {
			requestTastList() {
				let self = this;
				self.request({
					url: interfaces.welfare_task_list,
					data:{},
					method:"POST",
					success: (res)=>{
						// console.log("获取任务列表",res)
						if (res.code === config.SUCCESS) {
							let contentList = res.content
							contentList.forEach((item, index)=> {
								if (item.stype === 1) {
									self.localDataTaskOperation(item, tools.getTodayLike().length)
								} else if (item.stype === 2){
									self.localDataTaskOperation(item, tools.getTodayZhuiJu().length)
								} else if (item.stype === 5){
									let todayDingyueCount = tools.getTodayDingyue()
									self.localDataTaskOperation(item, todayDingyueCount)
								}
								let progress = item.user_number / item.number
								self.$set(item, "progress", progress)
							})
							self.dataList = self.dataList.concat(contentList)
						}
					}
				});
			},
			localDataTaskOperation(data, number){
				if (data.task_status == 0) {
					data.user_number = number
					if (data.user_number >= data.number) {
						data.user_number = data.number
						this.requestTaskFinish(data)
					}
				}
			},
			requestTaskFinish(data) {
				let self = this;
				self.request({
					url: interfaces.welfare_task_finish,
					data:{"task_id": data.id},
					method:"POST",
					success:(res)=> {
						console.log("任务完成接口:", res)
						if (res.code == config.SUCCESS) {
							data.task_status = 1
						}
					}
				})
			},
			requestReceiveReward(data, index){
				let self = this;
				self.request({
					url: interfaces.welfare_receiveReward,
					data:{"task_id": data.id},
					method:"POST",
					success:(res)=> {
						// console.log(res)
						var icon = 'success'
						if(res.code === config.SUCCESS) {
							let item = self.dataList[index]
							item.task_status = 2
							tools.get_user_info_by_serve(self,()=>{})
						} else {
							icon = 'error'
						}
						uni.showToast({
							icon: icon,
							title:res.message,
							duration:1500
						})
					}
				})
			},
			clickToButton(res, index) {
				// console.log(res)
				// task_status 状态 0 未完成 1 已经完成未领取 2 已经领取
				// stype 类型 1 点赞 2 新追剧 3 付费章节 4 充值 5 订阅
				if (res.task_status === 0) { //未完成
				    uni.switchTab({
				    	url:'/pages/tabBar/recommend/recommend'
				    })
				} else if (res.task_status === 1) { //已经完成未领取
				    this.requestReceiveReward(res, index)				    
				}
			}
		},
		mounted() {
			this.requestTastList()
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		box-sizing: border-box;
		padding: 0px 15px;
				
		.content{
			display: flex;
			box-sizing: border-box;
			flex-direction: column;
			border-radius: 10px;
			width: 100%;
			margin-bottom: 30rpx;
			background-color: white;
			
			.top_info{
				display: flex;
				box-sizing: border-box;
				justify-content: space-between;
				align-items: center;
				margin: 0px 15px;
				height: 100rpx;
				border-bottom-width: 1rpx;
				border-bottom-style: solid;
				border-bottom-color: #EFEFEF;
				.top_info_title{
					font-size: 17px;
					font-weight: 700;
				}
				.top_info_des{
					font-size: 14px;
					justify-content: right;
				}
			}
			
			.list_info{
				display: flex;
				box-sizing: border-box;
				flex-direction: column;
				margin: 0px 15px;
				margin-bottom: 30rpx;
				// background-color: red;
				.list_info_box{
					display: flex;
					box-sizing: border-box;
					height: 142rpx;
					margin-top: 40rpx;
					align-items: center;
					border-bottom-width: 1rpx;
					border-bottom-style: solid;
					border-bottom-color: #EFEFEF;
					// background-color: greenyellow;
					.list_info_box_icon{
						flex-shrink: 0;
						width: 94rpx;
						height: 94rpx;
						border-radius: 100%; //圆形
						margin-top: -20rpx;
						margin-right: 24rpx;
						.image{
							width: 100%;
							height: 100%;
							border-radius: 100%;
							// background-color: red;
						}
					}
					.list_info_box_details{
						display: flex;
						box-sizing: border-box;
						flex-direction: column;
						width: 100%;
						height: 100%;
						// background-color: yellow;
						
						.list_info_box_details_title{
							font-size: 14px;
							font-weight: 500;
							color: #3D3D3D;
						}
						.list_info_box_details_des{
							font-size: 13px;
							color: #969696;
							margin-top: 16rpx;
						}
						.list_info_box_details_progress{
							display: flex;
							box-sizing: border-box;
							height: 30rpx;
							align-items: center;
							margin-top: 15rpx;
							.list_info_box_details_progress_default{
								width: 200rpx;
								height: 10rpx;
								border-radius: 5rpx;
								background-color: #D8D8D8;
								.list_info_box_details_progress_progress{
									width: 100rpx;
									height: 10rpx;
									border-radius: 5rpx;
									background-color: #DF7C28;
								}
							}
							.list_info_box_details_progress_text{
								font-size: 11px;
								margin-left: 10rpx;
								color: #969696;
							}
							.list_info_box_details_progress_text1{
								color: #000000;
							}
						}						
					}
					.list_info_box_button_quwancheng{
						display: flex;
						box-sizing: border-box;
						width: 150rpx;
						height: 50rpx;
						border-radius: 30rpx;
						font-size: 12px;
						color: #ffffff;
						margin-left: 10rpx;
						justify-content: center;
						align-items: center;
						text-align: center;
						background-color: #D8D8D8;
					}
					.list_info_box_button_daiwancheng{
						background-color: #F6BC8A;
					}
					.list_info_box_button_lingkandian{
						background-color: red;
					}
					.list_info_box_button_yilingqu{
						background-color: #DF7C28;
					}
				}
			}
		}
	}
</style>